/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.ws.client.echo;

import java.io.FileOutputStream;
import java.util.Arrays;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import net.bioteam.appweb.TestConfig;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.ExecuteProgram;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.ExecuteProgramResponse;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.GetFile;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.GetFileResponse;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.GetJobFileAsZip;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.GetJobFileAsZipResponse;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.GetJobStatus;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.GetJobStatusResponse;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.JobStatusType;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.ListFiles;
import net.bioteam.appweb.ws.client.echo.ProgramwsStub.ListFilesResponse;


import org.apache.axis2.Constants;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;

import junit.framework.TestCase;

public class EchoTest extends TestCase
{
	net.bioteam.appweb.ws.client.echo.ProgramwsStub stub;

	@Override
	protected void setUp() throws Exception
	{

			stub = new net.bioteam.appweb.ws.client.echo.ProgramwsStub(null, TestConfig.serviceURL);

		ServiceClient sc = stub._getServiceClient();

		sc.engageModule("rampart");
		Options options = sc.getOptions();
		options.setUserName("test");
		options.setPassword("test");
		options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_FALSE);
	}

	public void testexecuteProgram() throws java.lang.Exception
	{

		ExecuteProgram program = new ExecuteProgram();
		program.setProgramName("echo");
		program.setSleep_time("60");
		program.set_cluster(-1);

		

		ExecuteProgramResponse resp =null;
		
		resp=stub.executeProgram(program);
		String jobid = resp.getJobid();
		System.out.println("jobid is " + resp.getJobid());
		System.out.println("workdir is " + resp.getWorkdir());
		GetJobStatus getJobStatus = new GetJobStatus();
		getJobStatus.setJobid(jobid);

		GetJobStatusResponse resp1 = stub.getJobStatus(getJobStatus);
		JobStatusType status = resp1.getStatus();
		System.out.println("status is " + status);

		ListFiles listFiles = new ListFiles();
		listFiles.setJobid(jobid);

		ListFilesResponse resp2 = stub.listFiles(listFiles);
		String[] fileNames = resp2.getFilename();
		System.out.println(Arrays.toString(fileNames));

		GetFile getFile = new GetFile();
		getFile.setJobid(jobid);
		getFile.setFileName("job_script.sh");

		GetFileResponse resp3 = stub.getFile(getFile);
		DataHandler dh1 = resp3.getContent();
		dh1.writeTo(System.out);
		
		GetJobFileAsZip zipfile=new GetJobFileAsZip();
		zipfile.setJobid(jobid);
		GetJobFileAsZipResponse resp4=stub.getJobFileAsZip(zipfile);
		DataHandler dh2=resp4.getZipfile();
		dh2.writeTo(new FileOutputStream("d:/tmp/job.zip"));
		
	}

}
